#-----------------------------------------------------------------------------
# Set policies as needed.
foreach(p
    CMP0020 # CMake 2.8.11
    CMP0022 # CMake 2.8.12
    CMP0025 # CMake 3.0
    CMP0043 # CMake 3.0
    CMP0053 # CMake 3.1
    )
  if(POLICY ${p})
    cmake_policy(SET ${p} NEW)
  endif()
endforeach()
#-----------------------------------------------------------------------------

PROJECT (QtVTKWikiExamples)

IF(NOT WikiExamples_BINARY_DIR)
FIND_PACKAGE(VTK REQUIRED)
IF(NOT VTK_USE_RENDERING)
  MESSAGE(FATAL_ERROR "Example ${PROJECT_NAME} requires VTK_USE_RENDERING.")
ENDIF(NOT VTK_USE_RENDERING)
INCLUDE(${VTK_USE_FILE})
ENDIF(NOT WikiExamples_BINARY_DIR)

if("${VTK_MAJOR_VERSION}.${VTK_MINOR_VERSION}" LESS 5.8)
  SET(KIT_LIBS vtkHybrid ${QT_LIBRARIES} )
else()
  SET(KIT_LIBS ${VTK_LIBRARIES})
endif()

# Let Qt find its MOCed headers in the build directory.
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})

#
# For all [example].ui files, build them together with their corresponding
# [example]Driver.cxx and [example].cxx. Subtract these cxx files from a
# list of all cxx files in the directory - we will build the rest in the next block.
FILE(GLOB ALL_UI_FILES *.ui)
FILE(GLOB ALL_FILES *.cxx)

include(${WikiExamples_SOURCE_DIR}/CMake/RequiresModule.cmake)

Requires_Module(ShareCameraQt vtkViewsQt)
Requires_Module(ShareCameraQtDriver vtkViewsQt)
Requires_Module(SideBySideRenderWindowsQt vtkViewsQt)
Requires_Module(SideBySideRenderWindowsQtDriver vtkViewsQt)
Requires_Module(QImageToImageSource vtkRenderingQt)

foreach(EXAMPLE_FILE ${ALL_UI_FILES})

  STRING(REPLACE ".ui" "" TMP ${EXAMPLE_FILE})

  STRING(REPLACE ${CMAKE_CURRENT_SOURCE_DIR}/ "" EXAMPLE ${TMP})
  set(UISrcs)
  set(MOCSrcs)

  if(VTK_QT_VERSION VERSION_GREATER "4")
    qt5_wrap_ui(UISrcs ${EXAMPLE}.ui)
    # CMAKE_AUTOMOC in ON so the MocHdrs will be automatically wrapped.
    add_executable(${WIKI}${EXAMPLE} ${EXECUTABLE_FLAG} ${EXAMPLE}Driver.cxx ${EXAMPLE}.cxx ${UISrcs} ${EXAMPLE}.h)
    qt5_use_modules(${WIKI}${EXAMPLE} Core Gui Widgets)
  else()
    QT4_WRAP_UI(UISrcs ${EXAMPLE}.ui)
    QT4_WRAP_CPP(MOCSrcs ${EXAMPLE}.h)

    ADD_EXECUTABLE(${WIKI}${EXAMPLE} ${EXECUTABLE_FLAG} ${EXAMPLE}Driver.cxx ${EXAMPLE}.cxx ${UISrcs} ${MOCSrcs})
  endif()
  TARGET_LINK_LIBRARIES(${WIKI}${EXAMPLE} ${KIT_LIBS})
  list(REMOVE_ITEM ALL_FILES ${CMAKE_CURRENT_SOURCE_DIR}/${EXAMPLE}Driver.cxx ${CMAKE_CURRENT_SOURCE_DIR}/${EXAMPLE}.cxx)
endforeach(EXAMPLE_FILE)

# Build all remaining .cxx files.
foreach(EXAMPLE_FILE ${ALL_FILES})
  STRING(REPLACE ".cxx" "" TMP ${EXAMPLE_FILE})
  STRING(REPLACE ${CMAKE_CURRENT_SOURCE_DIR}/ "" EXAMPLE ${TMP})

  if(VTK_QT_VERSION VERSION_GREATER "4")
    ADD_EXECUTABLE(${WIKI}${EXAMPLE} ${EXECUTABLE_FLAG} ${EXAMPLE}.cxx)
    qt5_use_modules(${WIKI}${EXAMPLE} Core Gui Widgets)
    TARGET_LINK_LIBRARIES(${WIKI}${EXAMPLE} ${KIT_LIBS})
  else()
    ADD_EXECUTABLE(${WIKI}${EXAMPLE} ${EXECUTABLE_FLAG} ${EXAMPLE}.cxx)
    TARGET_LINK_LIBRARIES(${WIKI}${EXAMPLE} ${KIT_LIBS})
  endif()
endforeach(EXAMPLE_FILE)
